package org.niugang.controller;


import org.niugang.entity.UserEntity;
import org.niugang.service.UserService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 如果其他资源服务器需要验证Token 则需要远程调用授权服务暴露的验证Token的API接口
     *
     * @param principal
     * @return
     */
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public Object getUser(Principal principal) {
        UserEntity queryList = userService.getUserByAccount(principal.getName());
        if(queryList!=null){
            Map<String, String> map = new LinkedHashMap<>();
            map.put("account", queryList.getAccount());
            map.put("name", queryList.getName());
            map.put("email", queryList.getEmail());
            map.put("id", queryList.getId() + "");
            map.put("phone", queryList.getPhone() + "");
            return map;
        }
        return null;

    }

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test() {
        return "test";
    }


}
